Module EntryPoint

    Private Declare Auto Function FindWindow Lib "user32" (ByVal lpClassName As String, _
                                                               ByVal lpWindowName As String) As IntPtr
    Private Declare Auto Function GetWindowText Lib "user32" (ByVal hwnd As IntPtr, _
                                                              ByVal lpString As String, _
                                                              ByVal cch As Integer) As Integer

    Private Const lpClassName = "Winamp v1.x"
    Private Const strTtlEnd = " - Winamp"

    Public Function GetWinampSong() As String
        Dim hwnd As IntPtr = FindWindow(lpClassName, vbNullString)
        If hwnd.Equals(IntPtr.Zero) Then Return "Not running"

        Dim lpText As New String(Chr(0), 100)
        Dim intLength As Integer = GetWindowText(hwnd, lpText, lpText.Length)

        If (intLength <= 0) OrElse (intLength > lpText.Length) Then Return "Unknown"

        Dim strTitle As String = lpText.Substring(0, intLength)
        Dim intName As Integer = strTitle.IndexOf(strTtlEnd)
        Dim intLeft As Integer = strTitle.IndexOf("[")
        Dim intRight As Integer = strTitle.IndexOf("]")

        If (intName >= 0) AndAlso (intLeft >= 0) AndAlso (intName < intLeft) AndAlso _
           (intRight >= 0) AndAlso (intLeft + 1 < intRight) Then _
            Return strTitle.Substring(intLeft + 1, intRight - intLeft - 1)

        If (strTitle.EndsWith(strTtlEnd)) AndAlso (strTitle.Length > strTtlEnd.Length) Then _
            strTitle = strTitle.Substring(0, strTitle.Length - strTtlEnd.Length)

        Dim intDot As Integer = strTitle.IndexOf(".")
        If (intDot > 0) AndAlso (IsNumeric(strTitle.Substring(0, intDot))) Then _
            strTitle = strTitle.Remove(0, intDot + 1)

        Return strTitle.Trim
    End Function

    Public Sub Main()
        Form1.ActiveForm.Activate()

    End Sub

End Module
